package com.hmall.api.config;

import cn.hutool.core.util.StrUtil;
import com.hmall.api.sentinel.ItemFallBackFactory;
import com.hmall.common.utils.UserContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DefaultFeignConfig {
    /**
     * OpenFeign的拦截器
     *
     * @return
     */
    @Bean
    public RequestInterceptor requestInterceptor() {
        return new RequestInterceptor() {
            @Override
            public void apply(RequestTemplate requestTemplate) {
                String userId = UserContext.getUser().toString();
                if (StrUtil.isNotEmpty(userId) && StrUtil.isNotBlank(userId)) {
                    requestTemplate.header("user-info", userId);
                }
            }
        };
    }

    @Bean
    public ItemFallBackFactory fallBackFactory() {
        return new ItemFallBackFactory();
    }
}
